package org.bmp.zemail.server.mail.services;

import microsoft.exchange.webservices.data.EmailMessage;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.Item;
import microsoft.exchange.webservices.data.ItemId;
import microsoft.exchange.webservices.data.ServiceLocalException;

import org.bmp.zemail.server.mail.models.Mail;
import org.springframework.stereotype.Service;

@Service
public class MailService {
	
	public final static String ACTION_NEXT = "next";
	public final static String ACTION_PREVIOUS = "previous";
	
	public Mail getMail(Item item, ExchangeService exchangeService) {
		try {
			EmailMessage message = EmailMessage.bind(exchangeService, 
					new ItemId(String.valueOf(item.getId().getUniqueId())));
			
			Mail mail = new Mail();
			mail.setId(message.getId().getUniqueId());
			mail.setFromAddress(message.getFrom().getAddress());
			mail.setFromName(message.getFrom().getName());
			mail.setSubject(message.getSubject());
			mail.setWhen(message.getDateTimeReceived());
			
			return mail;
			
		} catch (ServiceLocalException e) {
			// TODO BM problemas
			e.printStackTrace();
		} catch (Exception e) {
			// TODO BM problemas
			e.printStackTrace();
		}
		return null;
	}
}
